package com.wjs.blog.admin.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjs.blog.admin.mapper.PermissionMapper;
import com.wjs.blog.admin.model.params.PageParam;
import com.wjs.blog.admin.pojo.Permission;
import com.wjs.blog.admin.vo.PageResult;
import com.wjs.blog.admin.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PermissionServiceImp implements PermissionService{
    @Autowired
    PermissionMapper permissionMapper;
    @Override
    public Result listPermission(PageParam pageParam) {
        Page<Permission> permissionPage=new Page<>(pageParam.getCurrentPage(),pageParam.getPageSize());
        LambdaQueryWrapper<Permission> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(pageParam.getQueryString())){
            wrapper.eq(Permission::getName,pageParam.getQueryString());
        }
        Page<Permission> permissionPage1 = permissionMapper.selectPage(permissionPage, wrapper);
        PageResult<Permission> pageResult=new PageResult<>();
        pageResult.setList(permissionPage1.getRecords());
        pageResult.setTotal(permissionPage1.getTotal());
        return Result.success(pageResult);
    }
    @Override
    public Result add(Permission permission) {
        this.permissionMapper.insert(permission);
        return Result.success(null);
    }
    @Override
    public Result update(Permission permission) {
        this.permissionMapper.updateById(permission);
        return Result.success(null);
    }
    @Override
    public Result delete(Long id) {
        this.permissionMapper.deleteById(id);
        return Result.success(null);
    }
}
